﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Library.Actions;

namespace Eacademy.Areas.Admin.Controllers
{
    [CustomAuthorize(Roles = "Admin")]
    public class QuestionsController : Controller
    {
        private const int DefaultPageSize = 10;

        private IQuizRepository _quizRepsitory;
        private ISubjectRepository _subjectRepository;

        public QuestionsController(IQuizRepository quizRepository, ISubjectRepository subjectRepository)
        {
            _quizRepsitory = quizRepository;
            _subjectRepository = subjectRepository;

        }

        //
        // GET: /Admin/Quizzes/

        public ActionResult Index(int id,int subjectId,int mentoruserid, int? page)
        {
            if (id < 1)
            {
                return Redirect("/admin/subjects");
            }
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            QuestionListViewModel questionListViewModel = new QuestionListViewModel();
            int totalcount = 0;
            List<QuestionViewModel> questionViewModels = _quizRepsitory.GetQuestionsByQuizId(id,
                                                                                      currentPageIndex + 1,
                                                                                      DefaultPageSize, out totalcount);
            AdminSubjectsViewModel adminSubjectsViewModel = _subjectRepository.GetSubjectBySubjectId(subjectId);

            //questionViewModels.SubjectName = adminSubjectsViewModel.SubjectCode;
            questionListViewModel.QuizId = id;
            questionListViewModel.SubjectId = subjectId;
            questionListViewModel.MentorUserId = mentoruserid;
            questionListViewModel.QuestionViewModelList = questionViewModels.ToPagedList(currentPageIndex, DefaultPageSize, totalcount);

            return View(questionListViewModel);
        }

        public ActionResult Add(int id,int subjectid ,int mentoruserid)
        {
            //_quizRepsitory.AddQuiz(quizViewModel, id, mentoruserid);

            var choices = new ChoiceViewModel[]{
                new ChoiceViewModel{ ChoiceAnswer = "",IsAnswer = false},
                new ChoiceViewModel{ ChoiceAnswer = "",IsAnswer = false},
                new ChoiceViewModel{ ChoiceAnswer = "",IsAnswer = false},
                new ChoiceViewModel{ ChoiceAnswer = "",IsAnswer = false}
            };
            QuestionViewModel questionViewModel = new QuestionViewModel();
            questionViewModel.QuizId = id;
            questionViewModel.SubjectId = subjectid;
            questionViewModel.MentorUserId = mentoruserid;
            questionViewModel.Choices = choices.ToList();

            return View(questionViewModel);

        }

        [HttpPost]
        public ActionResult Add(QuestionViewModel questionViewModel)
        {

            if (_quizRepsitory.AddQuestion(questionViewModel))
            {
                this.FlashDone("The question has been added successfully");
            }
            else
            {
                this.FlashError("There was a problem creating the error");
            }
            return Redirect(Url.Action("Index", new {Controller = "Questions", Area = "Admin",id = questionViewModel.QuizId,mentoruserid = questionViewModel.MentorUserId,subjectid = questionViewModel.SubjectId}));
        }


        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (_quizRepsitory.DeleteQuestion(id))
            {
                this.FlashDone("Question deleted");
            }
            else
            {
                this.FlashError("Question was not deleted");
            }

            return Content("deleted");
        }

        public ActionResult Edit(int id,int mentoruserid,int quizid)
        {
            QuestionViewModel questionViewModel = _quizRepsitory.GetQuestionByQuestionId(id);
            questionViewModel.MentorUserId = mentoruserid;
            questionViewModel.QuestionId = id;
            questionViewModel.QuizId = quizid;
            return View(questionViewModel);
        }

        [HttpPost]
        public ActionResult Edit(QuestionViewModel questionViewModel)
        {
            if (_quizRepsitory.UpdateQuestion(questionViewModel))
            {
                this.FlashDone("The question has been updated");
            }
            else
            {
                this.FlashError("There was an error updating the question");
            }
            return Redirect(Url.Action("Index", new { Controller = "Questions", Area = "Admin", id = questionViewModel.QuizId, mentoruserid = questionViewModel.MentorUserId }));

        }

    }
}
